package com.zhc.dejingapi.utils.common;

import com.zhc.dejingapi.pojo.common.CommonUpdStatusConditionModel;
import com.zhc.dejingapi.pojo.common.CommonUpdStatusModel;
import com.zhc.dejingapi.pojo.common.CommonUpdV2StatusModel;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.Arrays;
import java.util.List;

public class CommomUpdAIMFieldStatusUtils {

    public static String getUpdAIMFieldValueNoCondtion(JdbcTemplate mySqlQFlowJdbcTemplate,
                                                       Long SourceForm_Id,
                                                       Long AimFormId,
                                                       List<String> listApplyId,
                                                       CommonUpdStatusModel updStatusModel
    ) throws Exception {


//    private String statusQueTitle;
//    private String compareSourceFiledQueTitle;//原应用字段
//    private String compareSourceFieldStaticType;//空值，直接对比，只有一条，sum ,agv
//    private String compareAimFieldQueTitle;//目标应用字段
//    private Long statusQueId;
//    private Long compareSourceFiledQueId;
//    private Long compareAimFieldQueId;
        String strSql = "SELECT que_id from question where form_id=? and que_title=?";

        Long lQueId = mySqlQFlowJdbcTemplate.queryForObject(strSql, Long.class, AimFormId, updStatusModel.getAimAppStatusQueTitle());

        updStatusModel.setStatusQueId(lQueId);

        lQueId = mySqlQFlowJdbcTemplate.queryForObject(strSql, Long.class, AimFormId, updStatusModel.getCompareAimFieldQueTitle());

        updStatusModel.setCompareAimFieldQueId(lQueId);

        lQueId = mySqlQFlowJdbcTemplate.queryForObject(strSql, Long.class, SourceForm_Id, updStatusModel.getCompareSourceFiledQueTitle());

        updStatusModel.setCompareSourceFiledQueId(lQueId);

        strSql = "SELECT que_type from question where form_id=? and que_title=?";
        Integer strSouceFieldQueType = mySqlQFlowJdbcTemplate.queryForObject(strSql, Integer.class, SourceForm_Id, updStatusModel.getCompareSourceFiledQueTitle());

        //strSql="SELECT que_id,que_type from question where form_id=? and que_title=?";

        //首先要获取目标字段的值

//        strSql="SELECT count(*) from data a \n" +
//                " LEFT JOIN apply b on a.apply_id=b.apply_id \n" +
//                " where b.being_deleted=0 \n" +
//                " and a.form_id=? and a.que_id=?";
//        int ihaveCount=mySqlQFlowJdbcTemplate.queryForObject(strSql,Integer.class,SourceForm_Id,strSouceQueId);


        //不判断目标数据的行是否存在

        //JdbcTemplate mySqlQFlowJdbcTemplate,
        //Long SourceForm_Id,
        //String sourceStaticType,
        //Boolean bFetchAll,
        //List<String> listApplyId,
        //Long strSouceQueId,
        //Integer strAIMQueType


        String strSouceFieldSumValue = CommomUpdAIMFieldValueUtils.getUpdAIMFieldValueNoCondtion(mySqlQFlowJdbcTemplate, SourceForm_Id,
                updStatusModel.getCompareSourceFieldStaticType(),
                false,
                listApplyId,
                updStatusModel.getCompareSourceFiledQueId(),
                strSouceFieldQueType);


        if (strSouceFieldSumValue == null || strSouceFieldSumValue.equals("")) {
            if (strSouceFieldQueType == 8) {
                strSouceFieldSumValue = "0";
            } else {
                strSouceFieldSumValue = "";
            }
        }


        strSql = "SELECT a.value \n" +
                " from data a \n" +
                " LEFT JOIN apply b on a.apply_id=b.apply_id \n" +
                " where b.being_deleted=0 \n" +
                " and a.form_id=? and a.que_id=?\n" +
                " ORDER BY a.value desc LIMIT 1";
        String strAIMFieldValue = mySqlQFlowJdbcTemplate.queryForObject(strSql,
                String.class,
                AimFormId, updStatusModel.getCompareAimFieldQueId());
        if (strAIMFieldValue == null || strAIMFieldValue.equals("")) {
            if (strSouceFieldQueType == 8) {
                strAIMFieldValue = "0";
            } else {
                strAIMFieldValue = "";
            }
        }


        String statusQueValue = "";
        if (strSouceFieldQueType == 8) {

            Double dSourceValue = Double.parseDouble(strSouceFieldSumValue);
            Double dAimValue = Double.parseDouble(strAIMFieldValue);

            for (int i = 0; i < updStatusModel.getStatusCondition().size(); i++) {

                CommonUpdStatusConditionModel commonUpdStatusConditionModel = updStatusModel.getStatusCondition().get(i);
                if (commonUpdStatusConditionModel.getIsCompareFixValue()) {

                    Double dAimValue1 = Double.parseDouble(commonUpdStatusConditionModel.getCompareFixValue());
                    if (CompareValue(dSourceValue, dAimValue1, commonUpdStatusConditionModel.getCompareCondition())) {
                        statusQueValue = commonUpdStatusConditionModel.getCompareValue();
                        break;
                    }

                } else {
                    if (CompareValue(dSourceValue, dAimValue, commonUpdStatusConditionModel.getCompareCondition())) {
                        statusQueValue = commonUpdStatusConditionModel.getCompareValue();
                        break;
                    }
                }

            }
        }
        else{
            for (int i = 0; i < updStatusModel.getStatusCondition().size(); i++) {

                CommonUpdStatusConditionModel commonUpdStatusConditionModel = updStatusModel.getStatusCondition().get(i);
                if (commonUpdStatusConditionModel.getIsCompareFixValue()) {


                    if (CompareValue(strSouceFieldSumValue, commonUpdStatusConditionModel.getCompareFixValue(),
                            commonUpdStatusConditionModel.getCompareCondition())) {
                        statusQueValue = commonUpdStatusConditionModel.getCompareValue();
                        break;
                    }

                } else {
                    if (CompareValue(strSouceFieldSumValue, strAIMFieldValue, commonUpdStatusConditionModel.getCompareCondition())) {
                        statusQueValue = commonUpdStatusConditionModel.getCompareValue();
                        break;
                    }
                }

            }
        }


        return statusQueValue;
    }


    public static String getUpdAIMFieldValueNoCondtionV2(JdbcTemplate mySqlQFlowJdbcTemplate,
                                                       Long SourceForm_Id,
                                                       Long AimFormId,
                                                       Long AimAppApplyId,
                                                       CommonUpdV2StatusModel updStatusModel
    ) throws Exception {


        String strSql="";
        String statusQueValue="";
        //把这两个值获取出来

        String strCompareValue0="";
        String strCompareValue1="";

        String strTableName="";
//        strSql="SELECT  trim(BOTH '\"' from json_extract(col6, '$[0].value')) as col6\n" +
//                "from large_data_1\n" +
//                "where being_deleted=0 and form_id=0 and apply_id=0";

        strSql=" SELECT count(*)\n" +
                "from `data` a \n" +
                "INNER JOIN apply b on a.apply_id=b.apply_id\n" +
                "where b.being_deleted=0 and a.form_id=? and a.que_id=? and a.apply_id=?\n";

        int iHaveCount=mySqlQFlowJdbcTemplate.queryForObject(strSql,Integer.class,
                AimFormId,
                updStatusModel.getCompareAimField0QueId(),
                AimAppApplyId);

        if(iHaveCount<1){
            strCompareValue0="";
        }
        else{
            strSql="SELECT IFNULL(`value`,'')\n" +
                    "from `data` a\n" +
                    "INNER JOIN apply b on a.apply_id=b.apply_id\n" +
                    "where b.being_deleted=0 and a.form_id=? and a.que_id=? and a.apply_id=?";

            strCompareValue0=mySqlQFlowJdbcTemplate.queryForObject(strSql,
                    String.class,
                    AimFormId,
                    updStatusModel.getCompareAimField0QueId(),
                    AimAppApplyId);
        }

        strSql=" SELECT count(*)\n" +
                "from `data` a \n" +
                "INNER JOIN apply b on a.apply_id=b.apply_id\n" +
                "where b.being_deleted=0 and a.form_id=? and a.que_id=? and a.apply_id=?\n";


        iHaveCount=mySqlQFlowJdbcTemplate.queryForObject(strSql,Integer.class,
                AimFormId,
                updStatusModel.getCompareAimField1QueId(),
                AimAppApplyId);

        if(iHaveCount<1){
            strCompareValue1="";
        }
        else{
            strSql="SELECT IFNULL(`value`,'')\n" +
                    "from `data` a\n" +
                    "INNER JOIN apply b on a.apply_id=b.apply_id\n" +
                    "where b.being_deleted=0 and a.form_id=? and a.que_id=? and a.apply_id=?";


            strCompareValue1=mySqlQFlowJdbcTemplate.queryForObject(strSql,
                    String.class,
                    AimFormId,
                    updStatusModel.getCompareAimField1QueId(),
                    AimAppApplyId);
        }

        if (strCompareValue0==null){
            strCompareValue0="";
        }
        if (strCompareValue1==null){
            strCompareValue1="";
        }
        //数据
        if (updStatusModel.getCompareAimField0QueType()==8){
            if (strCompareValue0.isEmpty()){
                strCompareValue0="0";
            }
            if (strCompareValue1.isEmpty()){
                strCompareValue1="0";
            }
            try{
                if (updStatusModel.getCompareAimField0QueType()<1){
                    //整数
                    Long iValue0=Long.parseLong(strCompareValue0);
                    Long iValue1=Long.parseLong(strCompareValue1);
                    for (int i = 0; i < updStatusModel.getStatusCondition().size(); i++) {

                        CommonUpdStatusConditionModel commonUpdStatusConditionModel = updStatusModel.getStatusCondition().get(i);
                        if (commonUpdStatusConditionModel.getIsCompareFixValue()) {

                            String strTmpValue=commonUpdStatusConditionModel.getCompareFixValue();
                            if (strTmpValue==null||strTmpValue.isEmpty()){
                                strTmpValue="0";

                            }
                            long lTmpValue=Long.parseLong(strTmpValue);

                            if (iValue1==lTmpValue) {
                                statusQueValue = commonUpdStatusConditionModel.getCompareValue();
                                break;
                            }
                        } else {
                            if (commonUpdStatusConditionModel.getCompareCondition().equals("=") || commonUpdStatusConditionModel.getCompareCondition().equals("==")) {
                                if (iValue0.equals(iValue1)) {
                                    statusQueValue = commonUpdStatusConditionModel.getCompareValue();
                                    break;
                                }
                            } else if (commonUpdStatusConditionModel.getCompareCondition().equals(">")) {
                                if (iValue0>iValue1) {
                                    statusQueValue = commonUpdStatusConditionModel.getCompareValue();
                                    break;
                                }
                            } else if (commonUpdStatusConditionModel.getCompareCondition().equals("<")) {
                                if (iValue0<iValue1) {
                                    statusQueValue = commonUpdStatusConditionModel.getCompareValue();
                                    break;
                                }
                            } else if (commonUpdStatusConditionModel.getCompareCondition().equals(">=")) {
                                if (iValue0>=iValue1) {
                                    statusQueValue = commonUpdStatusConditionModel.getCompareValue();
                                    break;
                                }
                            } else if (commonUpdStatusConditionModel.getCompareCondition().equals("<=")) {
                                if (iValue0<=iValue1) {
                                    statusQueValue = commonUpdStatusConditionModel.getCompareValue();
                                    break;
                                }
                            }
                            else    if (commonUpdStatusConditionModel.getCompareCondition().equals("!=") || commonUpdStatusConditionModel.getCompareCondition().equals("<>")) {
                                if (!iValue0.equals(iValue1)) {
                                    statusQueValue = commonUpdStatusConditionModel.getCompareValue();
                                    break;
                                }
                            }
                        }
                    }
                }
                else{
                    //小数

                    double dValue0=Double.parseDouble(strCompareValue0);
                    double dValue1=Double.parseDouble(strCompareValue1);
                    for (int i = 0; i < updStatusModel.getStatusCondition().size(); i++) {

                        CommonUpdStatusConditionModel commonUpdStatusConditionModel = updStatusModel.getStatusCondition().get(i);
                        if (commonUpdStatusConditionModel.getIsCompareFixValue()) {

                            double dAimValue1 = Double.parseDouble(commonUpdStatusConditionModel.getCompareFixValue());
                            if (CommonOrderBackToLakeUtils.doubleIsEqual(dValue1, dAimValue1)){
                                statusQueValue = commonUpdStatusConditionModel.getCompareValue();
                                break;
                            }

                        } else {
                            if (commonUpdStatusConditionModel.getCompareCondition().equals("=") || commonUpdStatusConditionModel.getCompareCondition().equals("==")) {
                                if (CommonOrderBackToLakeUtils.doubleIsEqual(dValue0, dValue1)) {
                                    statusQueValue = commonUpdStatusConditionModel.getCompareValue();
                                    break;
                                }
                            } else if (commonUpdStatusConditionModel.getCompareCondition().equals(">")) {
                                if (CommonOrderBackToLakeUtils.doubleIsGreaterThan(dValue0, dValue1)) {
                                    statusQueValue = commonUpdStatusConditionModel.getCompareValue();
                                    break;
                                }
                            } else if (commonUpdStatusConditionModel.getCompareCondition().equals("<")) {
                                if (CommonOrderBackToLakeUtils.doubleIsLessThan(dValue0, dValue1)) {
                                    statusQueValue = commonUpdStatusConditionModel.getCompareValue();
                                    break;
                                }
                            } else if (commonUpdStatusConditionModel.getCompareCondition().equals(">=")) {
                                if (CommonOrderBackToLakeUtils.doubleIsGreaterThanOrEqualTo(dValue0, dValue1)) {
                                    statusQueValue = commonUpdStatusConditionModel.getCompareValue();
                                    break;
                                }
                            } else if (commonUpdStatusConditionModel.getCompareCondition().equals("<=")) {
                                if (CommonOrderBackToLakeUtils.doubleIsLessThanOrEqualTo(dValue0, dValue1)) {
                                    statusQueValue = commonUpdStatusConditionModel.getCompareValue();
                                    break;
                                }
                            } else if (commonUpdStatusConditionModel.getCompareCondition().equals("!=") || commonUpdStatusConditionModel.getCompareCondition().equals("<>")) {
                                if (!CommonOrderBackToLakeUtils.doubleIsEqual(dValue0, dValue1)) {
                                    statusQueValue = commonUpdStatusConditionModel.getCompareValue();
                                    break;
                                }
                            }
                        }

                    }
                }
            }
            catch (Exception e){
                e.printStackTrace();
            }

        }
        else {
            //以字符串的方式进行
            for (int i = 0; i < updStatusModel.getStatusCondition().size(); i++) {

                CommonUpdStatusConditionModel commonUpdStatusConditionModel = updStatusModel.getStatusCondition().get(i);
                if (commonUpdStatusConditionModel.getIsCompareFixValue()) {

                    if (strCompareValue1.equals(commonUpdStatusConditionModel.getCompareFixValue())) {
                        statusQueValue = commonUpdStatusConditionModel.getCompareValue();
                        break;
                    }
                } else {
                    if (commonUpdStatusConditionModel.getCompareCondition().equals("=") || commonUpdStatusConditionModel.getCompareCondition().equals("==")) {
                        if (strCompareValue0.equals(strCompareValue1)) {
                            statusQueValue = commonUpdStatusConditionModel.getCompareValue();
                            break;
                        }
                    } else if (commonUpdStatusConditionModel.getCompareCondition().equals(">")) {
                        if (strCompareValue0.compareTo(strCompareValue1) > 0) {
                            statusQueValue = commonUpdStatusConditionModel.getCompareValue();
                            break;
                        }
                    } else if (commonUpdStatusConditionModel.getCompareCondition().equals("<")) {
                        if (strCompareValue0.compareTo(strCompareValue1) < 0) {
                            statusQueValue = commonUpdStatusConditionModel.getCompareValue();
                            break;
                        }
                    } else if (commonUpdStatusConditionModel.getCompareCondition().equals(">=")) {
                        if (strCompareValue0.compareTo(strCompareValue1) >= 0) {
                            statusQueValue = commonUpdStatusConditionModel.getCompareValue();
                            break;
                        }
                    } else if (commonUpdStatusConditionModel.getCompareCondition().equals("<=")) {
                        if (strCompareValue0.compareTo(strCompareValue1) <= 0) {
                            statusQueValue = commonUpdStatusConditionModel.getCompareValue();
                            break;
                        }
                    }
                    else    if (commonUpdStatusConditionModel.getCompareCondition().equals("!=") || commonUpdStatusConditionModel.getCompareCondition().equals("<>")) {
                        if (!strCompareValue0.equals(strCompareValue1)) {
                            statusQueValue = commonUpdStatusConditionModel.getCompareValue();
                            break;
                        }
                    }
                }
            }
        }


        return statusQueValue;
    }

    public static Boolean CompareValue(double strSouceValue, double strAimValue, String strCompareCondition) {
        Boolean bCompare = false;
        switch (strCompareCondition) {
            case ">":
                bCompare = strSouceValue > strAimValue;
                break;
            case ">=":
                bCompare = strSouceValue > strAimValue;
                break;
            case "<":
                bCompare = strSouceValue < strAimValue;
                break;
            case "<=":
                bCompare = strSouceValue < strAimValue;
                break;
            case "=":
                bCompare = strSouceValue == strAimValue;
                break;
            case "!=":
                bCompare = (strSouceValue != strAimValue);
                break;
            default:
                bCompare = false;
                break;
        }

        return bCompare;
    }

    public static Boolean CompareValue(String strSouceValue, String strAimValue, String strCompareCondition) {
        Boolean bCompare = false;

        if(strCompareCondition.toLowerCase().equals("in")){
            List<String> listArray= Arrays.asList(strAimValue.split(","));
            bCompare=listArray.contains(strSouceValue);

        }
        else  if(strCompareCondition.toLowerCase().equals("not in")){
            List<String> listArray= Arrays.asList(strAimValue.split(","));
            bCompare=!listArray.contains(strSouceValue);
        }
        else{
            int iCompareResult= strSouceValue.compareTo(strAimValue);
            switch (strCompareCondition) {
                case ">":
                    bCompare = iCompareResult > 0;
                    break;
                case ">=":
                    bCompare = iCompareResult >= 0;
                    break;
                case "<":
                    bCompare = iCompareResult < 0;
                    break;
                case "<=":
                    bCompare = iCompareResult <= 0;
                    break;
                case "=":
                   bCompare = strSouceValue.equals(strAimValue);
                   break;
                case "!=":
                    bCompare=!strSouceValue.equals(strAimValue);
                    break;

                default:
                    bCompare=false;
                    break;
            }
        }

        return false;
    }
}
